//
//  QRCodeViewController.swift
//  TTWeibo
//
//  Created by 唐磊guang on 16/7/6.
//  Copyright © 2016年 唐磊guang. All rights reserved.
//

import UIKit
import AVFoundation
class QRCodeViewController: UIViewController,UITabBarDelegate {

    @IBOutlet weak var tabBar: UITabBar!
    
    @IBOutlet weak var scanTopCons: NSLayoutConstraint!
    
    @IBOutlet weak var containHeightCons: NSLayoutConstraint!
    
    @IBOutlet weak var scanImageView: UIImageView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.delegate = self
        tabBar.selectedItem = tabBar.items![0]
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    override func viewDidAppear(animated: Bool){
        super.viewDidAppear(animated)
        startAnimation()
        
        startScan()
    }
    //扫描
    private func startScan(){
        if !session.canAddInput(deviceInput) {
            return
        }
        if !session.canAddOutput(self.outPut) {
            return
        }
        //将输入输出都添加到会话中
        session.addInput(deviceInput)
         print(outPut.availableMetadataObjectTypes)
        session.addOutput(outPut)
         print(outPut.availableMetadataObjectTypes)
        
        //设置输出能够解析的数据类型
        //设置能够解析的数据类型必须在被添加到会话之后，否则会报错
        outPut.metadataObjectTypes = outPut.availableMetadataObjectTypes
        print(outPut.availableMetadataObjectTypes)
        //设置输出对象的代理
        outPut.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
        //添加预览图层
        view.layer.insertSublayer(previewLayer, atIndex: 0)
        //告诉session开始扫描
        session.startRunning()
    }
    
    //开始动画
    private func startAnimation(){
        scanTopCons.constant = -containHeightCons.constant
        view.layoutIfNeeded()
        
        UIView.animateWithDuration(2.0) {
            //设置动画指定的次数
            UIView.setAnimationRepeatCount(MAXFLOAT)
            //修改约束
            self.scanTopCons.constant = 0
            //强制更新界面
            self.view.layoutIfNeeded()
        }
    }
    //返回按钮点击
    @IBAction func cancelButtonClick(sender: AnyObject) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    //Mark --UITabBarDelegate
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem){
        //修改容器高度
        if item.tag == 1 {
            containHeightCons.constant = 300
        }else{
            containHeightCons.constant = 150
        }
        //停止动画
        scanImageView.layer.removeAllAnimations()
        //重新开始动画
        startAnimation()
    }
    //Mark -- 懒加载
    //会话
    private lazy var session: AVCaptureSession = AVCaptureSession()
    //拿到输入设备
    private lazy var deviceInput:AVCaptureDeviceInput? = {
        //获取摄像头
        let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        do{
            let input = try AVCaptureDeviceInput(device: device)
            return input
        }catch{
            print(error)
            return nil
        }
    }()
    //拿到输出对象
    private lazy var outPut:AVCaptureMetadataOutput = AVCaptureMetadataOutput()
    //创建预览图层
    private lazy var previewLayer: AVCaptureVideoPreviewLayer = {
        let layer = AVCaptureVideoPreviewLayer(session: self.session)
        layer.frame = UIScreen.mainScreen().bounds
        return layer
    }()
}
extension QRCodeViewController: AVCaptureMetadataOutputObjectsDelegate{
    func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!){
        
    }
}

